package com.qlm.tools;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.Collections;
import java.util.Enumeration;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Properties;
import java.util.Set;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class OrderedProperties extends Properties {
	protected final static Logger       logger = LoggerFactory.getLogger(OrderedProperties.class); 
	
    private static final long serialVersionUID = -4627607243846121965L;
    private final LinkedHashSet<Object> keys = new LinkedHashSet<Object>();
      
    private static  String filename = "editconfig.properties";
    private static String filepath = OrderedProperties.class.getClassLoader().getResource(filename).getPath();
    
    public Enumeration<Object> keys() {
        return Collections.<Object> enumeration(keys);
    }

    public Object put(Object key, Object value) {
        keys.add(key);
        return super.put(key, value);
    }
    
    public synchronized Object remove(Object key) {
        keys.remove(key);
        return super.remove(key);
    }

    public Set<Object> keySet() {
        return keys;
    }

    public Set<String> stringPropertyNames() {
        Set<String> set = new LinkedHashSet<String>();
        for (Object key : this.keys) {
            set.add((String) key);
        }
        return set;

    }
    public static Properties getProperties(){
    	return OrderedProperties.readPropertiesFileObj(filepath);
    };
 // 读取资源文件,并处理中文乱码
    public static Properties readPropertiesFileObj(String filepath) {
    	logger.debug(filepath);
        Properties properties = new OrderedProperties();
        try {
            InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(filename);
            properties.load(new InputStreamReader(inputStream, "utf-8"));
            BufferedReader bf = new BufferedReader(new InputStreamReader(inputStream));
            properties.load(bf);
            inputStream.close(); // 关闭流
        } catch (IOException e) {
            e.printStackTrace();
        }
        return properties;
    }

    /**
     * 写资源文件，含中文
     * @param properties
     * @param map 修改值的集合
     * @param remark修改说明（例：update score config/update hb config）
     */
    public static void writePropertiesFileObj(Properties properties,Map<String, String> map,String remark) {
    	logger.debug(filepath);
    	if (properties == null) {
            properties = new OrderedProperties();
        }
        try {
            OutputStream outputStream = new FileOutputStream(filepath);
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(outputStream, "utf-8"));
            for (Map.Entry<String, String> entry : map.entrySet()) {
        		   properties.setProperty(entry.getKey(), entry.getValue());
			}
            properties.store(bw, remark);
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
